package widget.badge;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import library.utils.DisplayUtil;
import widget.utils.ViewUtils;


/**
 * Created by Mr.Fan on 2016/5/30.
 */
public class BadgeView extends View {
    private Paint paint;
    private int textSize;
    private int fontPadding;
    private String text;
    private int badgeColor = Color.RED;
    private int textColor = Color.WHITE;

    public BadgeView(Context context) {
        super(context);
        init();
    }

    public BadgeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public BadgeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(21)
    public BadgeView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {

        textSize = DisplayUtil.sp2px(12);
        fontPadding = DisplayUtil.dp2px(2);

        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);//充满
        paint.setAntiAlias(true);//
        paint.setFilterBitmap(true);
    }

    private int measureHeight(int heightMeasureSpec) {
        final int minimumHeight = getSuggestedMinimumHeight();
        final int textHeight = textSize;
        int computedHeight = resolveMeasured(heightMeasureSpec, textHeight);
        return computedHeight;
    }

    private int measureWidth(int widthMeasureSpec) {
        final int minimumWidth = getSuggestedMinimumWidth();
        int textWidth = minimumWidth;
        if (!TextUtils.isEmpty(text)) {
            paint.setTextSize(textSize);
            textWidth = (int) paint.measureText(text) + fontPadding * 2;
        }
        int computedWidth = resolveMeasured(widthMeasureSpec, textWidth);
        return computedWidth;
    }

    private int resolveMeasured(int measureSpec, int desired) {
        int result;
        int specSize = MeasureSpec.getSize(measureSpec);
        int specMode = MeasureSpec.getMode(measureSpec);
        switch (specMode) {
            case MeasureSpec.UNSPECIFIED:
                result = desired;
                break;
            case MeasureSpec.AT_MOST:
                result = Math.min(specSize, desired);
                break;
            case MeasureSpec.EXACTLY:
            default:
                result = specSize;
        }
        return result;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = measureWidth(widthMeasureSpec);
        int height = measureHeight(heightMeasureSpec);
        if (width < height)
            width = height;
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        final int width = getWidth();
        final int height = getHeight();

        final RectF rectF = new RectF(0, 0, width, height);

        paint.setColor(badgeColor);
        float r = rectF.height() / 2;
        if (!TextUtils.isEmpty(text)) {
            canvas.drawRoundRect(rectF, r, r, paint);
        } else {
            canvas.drawCircle(rectF.centerX(), rectF.centerY(), r, paint);
        }

        if (!TextUtils.isEmpty(text)) {
            int size = height - fontPadding;
            size = textSize > size ? size : textSize;
            paint.setColor(textColor);
            paint.setTextSize(size);
            ViewUtils.drawTextInRect(canvas, rectF, text, paint);
        }
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        invalidate();
    }

    public int getBadgeColor() {
        return badgeColor;
    }

    public void setBadgeColor(int badgeColor) {
        this.badgeColor = badgeColor;
        invalidate();
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
        invalidate();
        requestLayout();
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = DisplayUtil.sp2px(textSize);
        invalidate();
        requestLayout();
    }

    public boolean bezier(final BadgeBezier.OnDismissListener onDismissListener) {
        return BadgeBezier.attach(this, onDismissListener);
    }


    /**
     * @param count
     * @param plus  if(count>plus), show as "plus+"
     */
    public void setCount(long count, long plus) {
        setVisibility(count < 1 ? INVISIBLE : VISIBLE);
        if (count < 1) {
            setText(String.valueOf(count));
        } else if (count > plus) {
            setText(String.valueOf(plus) + "+");
        } else {
            setText(String.valueOf(count));
        }
    }
}
